package com.jinbo.footmessageworker.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.utils.DialogUtils;
import com.jinbo.footmessageworker.utils.TimeCount;
import com.jinbo.footmessageworker.utils.VerifyUtils;
import com.jinbo.footmessageworker.view.CrossEditText;
import com.jinbo.footmessageworker.view.TitleBarView;


public class FindPasswordActivity extends BaseActivity implements View.OnClickListener {
    private CrossEditText mPhoneEt,mPasswordEt,mCodeEt,mConPasswordEt;
    private Button mGetCodeBt,mSubmit;
    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, FindPasswordActivity.class);
        activity.startActivity(intent);
    }

/*    public static final int COUNT = 60;
    private Button submit, mGetCodeBt;
    private CrossEditText mPhoneEt, mCodeEt, mEmailEt, mPasswordEt;
    private TextView mCountTv;
    private LinearLayout mCountLL, mPhoneLL;
    private LocalPreferencesHelper localPreferencesHelper;
    private TitleBarView tbv_titleBar;*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        initTitleBarView();
        initView();
        initListener();
   /*     localPreferencesHelper = new LocalPreferencesHelper(this, LocalPreferencesHelper.DB_NAME);
      */
    }

    public void initTitleBarView() {
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("找回密码");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
    }

    // 初始化View
    private void initView() {
        mPhoneEt = (CrossEditText) findViewById(R.id.phone_num);
        mGetCodeBt = (Button) findViewById(R.id.get_code);
        mSubmit = (Button) findViewById(R.id.submit);
        mPasswordEt = (CrossEditText) findViewById(R.id.re_pwd);
        mConPasswordEt= (CrossEditText) findViewById(R.id.con_pwd);
        mCodeEt = (CrossEditText) findViewById(R.id.code_num);
/*        submit = (Button) findViewById(R.id.submit);
        mCodeEt = (CrossEditText) findViewById(R.id.msg_code);
        mEmailEt = (CrossEditText) findViewById(R.id.email);
        mPasswordEt = (CrossEditText) findViewById(R.id.reg_pwd);
        mCountTv = (TextView) findViewById(R.id.second);
        mCountLL = (LinearLayout) findViewById(R.id.ll_count);
        mPhoneEt.requestFocus();
        mPhoneLL = (LinearLayout) findViewById(R.id.ll_phone);
        mPhoneLL.setBackgroundResource(R.drawable.shape_edittext_focused);*/
    }


    // 监听
    private void initListener() {
        //submit.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
        mGetCodeBt.setOnClickListener(this);
        mPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mPhoneEt.getText().toString().trim().length() == 11) {
                        mGetCodeBt.setEnabled(true);
                } else {
                    mGetCodeBt.setEnabled(false);
                }

                submitEnable();

            }
        });
        mCodeEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mPasswordEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mConPasswordEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
    }
    void submitEnable()
    {
        if (mPhoneEt.getText().toString().trim().length() == 11
                && mPasswordEt.getText().toString().trim().length() > 0&&mConPasswordEt.getText().toString().trim().length() > 0
                && mCodeEt.getText().toString().trim().length() > 0) {
            mSubmit.setEnabled(true);
        } else {
            mSubmit.setEnabled(false);
        }
    }

    private void submitCheck() {
        String password = mPasswordEt.getText().toString().trim();
        String confirm=mConPasswordEt.getText().toString().trim();
        if (TextUtils.isEmpty(password) || !VerifyUtils.isPassword(password) || password.length() > 16 || password.length() < 6) {
            show("密码格式为6-16位数字或字母组合");
            return;
        }
        else if(!password.equals(confirm))
        {
            show("两次输入密码不一致");
            return;
        }
        else {
            new DialogUtils(FindPasswordActivity.this, "center",false,0)
                    .setMessage("修改密码成功！请就住新密码")
                    .setButtons(null, "返回登录",
                            new DialogUtils.OnDialogButtonClickListener() {
                                @Override
                                public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                    if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {
                                        // ActivitiesHelper.getInstance().closeAll();
                                        LoginActivity.launch(FindPasswordActivity.this);
                                    }
                                }
                            }).create().show();
         //   LoginActivity.launch(FindPasswordActivity.this);
      /*      UserBusinessController.getInstance().checkCode(phone, code, 1, new Listener<CheckCodeBean>() {
                @Override
                public void onStart(Object... params) {
                    showProgressDialog("请稍后...");
                }

                @Override
                public void onComplete(CheckCodeBean result, Object... params) {
                    dismissProgressDialog();
//                //跳转到下个界面
                    SettingNewPasswordActivity.launch(FindPasswordActivity.this,result.code);
                }

                @Override
                public void onFail(String msg, Object... params) {
                    if (TextUtils.isEmpty(msg)) {
                    } else {
                        dismissProgressDialog();
                        new DialogUtils(FindPasswordActivity.this, "center",true).setMessage(msg).setButtons(null, "确定", null).create().show();

                    }
                }
            });*/
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
           case R.id.submit:
                submitCheck();
                break;
            case R.id.get_code:
                mGetCodeBt.setEnabled(false);
                getCode();
                break;
            default:
                break;
        }
    }


    public void getCode() {
        String phone = mPhoneEt.getText().toString().trim();
       if (!VerifyUtils.isPhoneNum(phone)) {
            //弹出提示 您输入的手机号码格式错误！
            show("您输入的手机号码格式错误！");
            return;
        } else {
           TimeCount time = new TimeCount(60000, 1000,mGetCodeBt);//构造CountDownTimer对象
           time.start();
           show("验证码已发送，请注意查收");
           return;
        /*    UserBusinessController.getInstance().sendCode(phone, null, 2, new Listener<SendCodeBean>() {

                @Override
                public void onStart(Object... params) {
                    showProgressDialog("请稍后...");
                }

                @Override
                public void onComplete(SendCodeBean result, Object... params) {
                    dismissProgressDialog();
                    countDown(COUNT);
                    mGetCodeBt.setEnabled(false);
                    mCountLL.setVisibility(View.VISIBLE);
                    ToastUtils.show(RegisterActivity.this, "验证码已发送，请注意查收");
                }

                @Override
                public void onFail(String msg, Object... params) {
                    dismissProgressDialog();
                    if (TextUtils.isEmpty(msg)) {
                    } else {
                        new DialogUtils(RegisterActivity.this, "center", true).setMessage(msg).setButtons(null, "确定", null).create().show();
                        ;

                    }
                }
            });*/
        }
    }
}
